package com.pshcp.patient.api;

import com.external.pay.PayService;
import com.external.pay.domain.PayPackageDTO;
import com.external.service.WsRequestService;
import com.external.ws.soap.entity.dto.UnpaidFeeViewDTO;
import com.external.ws.soap.entity.res.UnpaidFeeListRes;
import com.external.ws.soap.entity.res.UnpaidFeeViewRes;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.domain.model.Patient;
import com.pshcp.domain.model.PatientUnpayFeeRecord;
import com.pshcp.domain.model.PatientUser;
import com.pshcp.patient.business.PatientUnpaidFeeBusiness;
import com.pshcp.patient.domain.patientUnpaid.ao.UnpaidDetailAO;
import com.pshcp.patient.domain.patientUnpaid.ao.UnpaidListAO;
import com.pshcp.patient.domain.patientUnpaid.ao.UnpaidCreateAO;
import com.pshcp.patient.domain.patientUnpaid.ao.UnpaidToPayAO;
import com.pshcp.patient.domain.patientUnpaid.vo.UnpaidDetailVO;
import com.pshcp.patient.domain.patientUnpaid.vo.UnpaidListVO;
import com.pshcp.patient.tool.PatientUnpaidTools;
import com.pshcp.service.PatientService;
import com.pshcp.service.PatientUnpayFeeRecordService;
import com.pshcp.service.PatientUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Api(tags = {"患者端-更多-自助缴费"})
@Slf4j
@RestController
@RequestMapping("/api/patient/unpaid")
public class PatientUnpaidFeeAPI {

    @Autowired
    private PatientUnpayFeeRecordService unpayFeeRecordService;

    @Autowired
    private PatientUnpaidFeeBusiness unpaidFeeBusiness;

    @Autowired
    private WsRequestService wsRequestService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private PayService payService;

    @Autowired
    private PatientUserService patientUserService;

    @ApiOperation("诊间待付费用列表")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "诊间待付费用列表")
    @PostMapping("/list")
    public ResultDTO<UnpaidListVO> list(@RequestBody UnpaidListAO listAO) throws BizException {
        Patient patient = patientService.findById(listAO.getPatientId());
        CommonTool.checkObj(patient, "10180001");
        UnpaidFeeListRes res = wsRequestService.unpaidFeeList(PatientUnpaidTools.buildWsListDTO(patient, listAO));
        return ResultBuilder.success(PatientUnpaidTools.buildListVO(res));
    }

    @ApiOperation("诊间待付费用详情")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "诊间待付费用详情")
    @PostMapping("/detail")
    public ResultDTO<UnpaidDetailVO> detail(@RequestBody UnpaidDetailAO detailAO) throws BizException {
        Patient patient = patientService.findById(detailAO.getPatientId());
        CommonTool.checkObj(patient, "10180001");
        UnpaidFeeViewDTO viewDTO = PatientUnpaidTools.buildWsViewDTO(patient.getHisPatientId(), detailAO.getAccUnpaidFeeId());
        UnpaidFeeViewRes res = wsRequestService.unpaidFeeView(viewDTO);
        return ResultBuilder.success(PatientUnpaidTools.buildDetailVO(res));
    }

    @ApiOperation("诊间支付订单确认")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.ADD, desc = "诊间支付订单确认")
    @PostMapping("/create")
    public ResultDTO create(@RequestBody UnpaidCreateAO createAO) throws BizException {
        Patient patient = patientService.findById(createAO.getPatientId());
        CommonTool.checkObj(patient, "10180001");
        UnpaidFeeViewDTO viewDTO = PatientUnpaidTools.buildWsViewDTO(patient.getHisPatientId(), createAO.getAccUnpaidFeeId());
        UnpaidFeeViewRes res = wsRequestService.unpaidFeeView(viewDTO);
        CommonTool.checkObj(res.getAccUnpaidFeeId(), "10220001");
        // exist unpaid record
        PatientUnpayFeeRecord feeRecord = unpayFeeRecordService.findByAccFeeId(res.getAccUnpaidFeeId());
        if(feeRecord != null){
            return ResultBuilder.success(feeRecord.getId());
        }
        PatientUnpayFeeRecord record = PatientUnpaidTools.buildCreateModel(patient, res);
        unpayFeeRecordService.insert(record);
        return ResultBuilder.success(record.getId());
    }

    @ApiOperation("诊间支付订单去支付")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.ADD, desc = "诊间支付订单去支付")
    @PostMapping("/toPay")
    public ResultDTO<PayPackageDTO> toPay(@RequestBody UnpaidToPayAO toPayAO) throws BizException {
        PatientUnpayFeeRecord feeRecord = unpayFeeRecordService.findById(toPayAO.getId());
        CommonTool.checkObj(feeRecord, "10220002");
        if(!RegPayStatusEnum.WAIT_PAY.getCode().equals(feeRecord.getPayStatus())){
            return ResultBuilder.failure("10220003");
        }
        if(BigDecimal.ZERO.compareTo(feeRecord.getTotalFee()) >= 0){
            unpaidFeeBusiness.enterOrder(feeRecord, null, null);
            PayPackageDTO payPackageDTO = new PayPackageDTO();
            payPackageDTO.setSkipPay(true);
            return ResultBuilder.success(payPackageDTO);
        }
        PatientUser patientUser = patientUserService.findById(ReqComHold.getReqCom().getUserId());
        toPayAO.setOpenid(patientUser.getOpenid());

        PayPackageDTO payPackageDTO = payService.callPay(PatientUnpaidTools.buildPayDTO(feeRecord,toPayAO));
        return ResultBuilder.success(payPackageDTO);
    }

}
